package com.xc.tjise.TJISE.day10;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
// Ctrl Alt o  去掉没用的包 packet
public class CSDN {
    public static  final int BATCH_SIZE=100000;
    private static final String SQL = "INSERT INTO db_csdn.csdn VALUES(NULL, ?, ?, ?)";
    public static void main(String[] args) throws SQLException, IOException {


        Connection connection = DBCPDatasource.getconnection();
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement(SQL);
//        System.out.println(connection);
        // 把 www.csdn.net.sql 的所有数据导入 db_csdn.csdn 表

        BufferedReader bufferedReader = new BufferedReader(new FileReader("www.csdn.net.sql"));



        int counter = 0;
        String line;
        String[] strings;
        long sartTime = System.currentTimeMillis();
        while ((line=bufferedReader.readLine())!=null){
            strings = line.split(" # ");
            String username  = strings[0];
            String password  = strings[1];
            String email     = strings[2];
          //  System.out.println(username+"  "+password+"  "+email);


            //保存到MySQL
//            PreparedStatement preparedStatement =connection.prepareStatement(SQL);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            preparedStatement.setString(3,email);
            preparedStatement.addBatch();
            counter++;
            if (counter % BATCH_SIZE == 0) {
                preparedStatement.executeBatch();
                System.out.println(counter + " data inserted.");
            }
        }
        preparedStatement.executeBatch();


        connection.commit();


        long endTime = System.currentTimeMillis();

        preparedStatement.close();
        connection.close();

        System.out.println("total datas: " + counter);
        System.out.println((endTime - sartTime) + " ms.");



            //更新
           // preparedStatement.executeUpdate();



        }
    }

